package com.kxmall.web.controller.categorize;

import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.categorize.domain.vo.HsWasteCategorizeVo;
import com.kxmall.categorize.service.IHsWasteCategorizeService;
import com.kxmall.common.core.controller.BaseAppController;
import com.kxmall.common.core.domain.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 废品分类信息
 *
 * @author kxmall
 * @date 2023-11-21
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/categorize/app")
public class HsAppCategorizeController extends BaseAppController {

    private final IHsWasteCategorizeService iHsWasteCategorizeService;

    /**
     * 查询废品分类信息列表
     */
    @SaIgnore
    @GetMapping("/allList")
    public R<List<HsWasteCategorizeVo>> list() {
        List<HsWasteCategorizeVo> dataList = iHsWasteCategorizeService.queryListByParentId(0l);
        if(dataList != null){
            for(HsWasteCategorizeVo parentHsWasteCategorizeVo : dataList){
                List<HsWasteCategorizeVo> childDataList = iHsWasteCategorizeService.queryListByParentId(parentHsWasteCategorizeVo.getId());
                if(childDataList != null){
                    parentHsWasteCategorizeVo.setChildCategorizeVoList(childDataList);
                }
            }

            return R.ok(dataList);
        }

        return R.fail("为获取到分类");
    }


    /**
     * 根据上级id获取废品分类二级列表信息
     *
     * @param id 主键
     */
    @SaIgnore
    @GetMapping("/listById/{id}")
    public R<List<HsWasteCategorizeVo>> getListInfoById(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iHsWasteCategorizeService.queryListByParentId(id));
    }


    /**
     * 获取废品分类信息详细信息
     *
     * @param id 主键
     */
    @SaIgnore
    @GetMapping("/{id}")
    public R<HsWasteCategorizeVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(iHsWasteCategorizeService.queryById(id));
    }


}
